const router = require('express').Router()
const { generateToken } = require('../utils/auth');

// 用户登录
router.post('/login', (req, res) => {
  // 获取请求参数
  const { username, password } = req.body
  let permission = {}
  if (username == 'admin') {
     permission = {
      name: '管理员',
      avatar:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F3f580f26-0244-4b56-81e4-dd2bf777aa77%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1714572467&t=7bf99719872fcab1d99ac7bfbd34e26e',
       routes: [
        {
        id: 1,
        name: 'home',
        path: '/home',
        component:'home',
        meta: {
              title: '首页',
              icon: 'icon-jurassic_user'}
         },
         {
        id: 2,
        name: 'setting',
        path: '/setting',
        component:'setting',
        meta: {
              title: '设置',
              icon: 'icon-jurassic_user'}
        },
        ]
    }
  } else if(username == 'user') {
      permission = {
        name: '普通用户',
        avatar:'https://img1.baidu.com/it/u=739064432,179228396&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500',
        routes: [
        {
          name: '首页',
          path: '/home'
        }
        ]
    }
  } else {
   return res.send({
      code: 500,
      msg: '用户名或密码错误'
    })
  }
  res.send({
    code:200,
    msg:'登录成功',
    data: {
      permission,
      token: generateToken({ username, password }),
      username: username
    }
  })
})
  




module.exports =router